//
//  HMDetailsTableViewController.m
//  subscribe
//
//  Created by 宋轩 on 17/1/1.
//  Copyright © 2017年 song. All rights reserved.
//

#import "HMDetailsTableViewController.h"
#import "HMTryReadTableViewController.h"
#import <SVProgressHUD.h>
#import <UIImageView+WebCache.h>
#import "HMUPgradeViewModel.h"
#import "HMUpgradeTableViewCell.h"
#import "HMSuitableModel.h"
#import "HMDetailsHeaderView.h"
#import "HMShoppingCarTableViewController.h"
#import "MOPopWindow.h"
#import "HMPayViewController.h"
#import "UIViewController+Extension.h"

#define HEADER_HEINGT 331
#define SCREEN_WEIGHT self.view.bounds.size.width
#define SCREEN_HEIGHT self.view.bounds.size.height
#define NAV_HEIGHT 64

@interface HMDetailsTableViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *detailsTableView;
@property (nonatomic, strong) UIView *navigationBarView;
@property (nonatomic, assign) CGFloat offset;
@property (nonatomic, strong) HMSuitableModel *dataModel;
@property (nonatomic, strong) HMDetailsHeaderView *detailsHeaderView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *btnView;


@end

//注册ID
static NSString *detailscellID = @"DetailscellID";
static NSString *suitablelID = @"SuitablelID";
static NSString *upgradeID = @"UpgradeID";

@implementation HMDetailsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavUI];
    [self loadDetailsData];
    
    //创建详情界面
    [self createDetailsTableView];
    
    //默认偏移关闭
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建头部
    [self createHeaderView];
    
    //创建底部
    [self createChooseVeiw];
    
    [self creataNavigationBarView];
    
    self.navigationBarView.alpha = 0;
    
    //-------------设置导航栏开始--------------------------------
    [super setupNavRightItemsByOne];
    //-------------设置导航栏结束--------------------------------
}

- (void)loadDetailsData
{
    [HMUPgradeViewModel getUPgradeModeWithID:self.tid Finished:^(id arr) {
        _dataModel = arr;
        self.detailsHeaderView.suitableModel = self.dataModel;
        self.titleLabel.text = self.dataModel.name;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
        [self.detailsTableView reloadData];
    } Failed:^(NSError *error) {
        NSLog(@"------------%@",error);
    }];
}


- (void)setNavUI {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"btn_backItem"] forState:UIControlStateNormal];
    button.adjustsImageWhenHighlighted = NO;
    NSString *title = self.navigationController.viewControllers.firstObject.title;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];

    
    [button sizeToFit];
    
    [button addTarget:self action:@selector(btnLeftAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
}

- (void)btnLeftAction {
  
//    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:nil];
//    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;

    [self.navigationController popViewControllerAnimated:YES];
}




- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
////    //设置导航栏透明
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    

}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    [super viewWillDisappear:animated];
    
    
}
//导航栏透明
- (void)creataNavigationBarView
{
    UIView *navigationBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WEIGHT, NAV_HEIGHT)];
    navigationBarView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:navigationBarView];

    UILabel *titleLabel = [UILabel hm_labelWithText:self.dataModel.name fontSize:17 color:[UIColor blackColor]];
    self.titleLabel = titleLabel;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [navigationBarView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(44);
    }];
    
    self.navigationBarView = navigationBarView;
    
}

//创建头部视图
- (void)createHeaderView
{
    
    HMDetailsHeaderView *detailsHeaderView = [HMDetailsHeaderView createHeaderView];
    self.detailsHeaderView = detailsHeaderView;
    
    self.detailsTableView.tableHeaderView = detailsHeaderView;
    
    [detailsHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).offset(0);
//        make.height.offset(600);
        make.bottom.equalTo(detailsHeaderView.subscribe_infoLabel.mas_bottom).offset(20);
    }];
    
//    [self.view layoutIfNeeded];

}
#pragma mark - Table view data source


//创建详情界面
- (void)createDetailsTableView
{
    UITableView *detailsTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    //额外滚动距离加大
    detailsTableView.contentInset = UIEdgeInsetsMake(0, 0, 150, 0);
    
    [self.view addSubview:detailsTableView];
    
    self.detailsTableView = detailsTableView;
    
    //预估行高
    detailsTableView.estimatedRowHeight = 200;
    detailsTableView.rowHeight = UITableViewAutomaticDimension;
    
    detailsTableView.delegate = self;
    detailsTableView.dataSource = self;
    
    //注册
    [detailsTableView registerNib:[UINib nibWithNibName:@"HMIntroductionTableViewCell" bundle:nil] forCellReuseIdentifier:detailscellID];
    [detailsTableView registerNib:[UINib nibWithNibName:@"HMSuitableTableViewCell" bundle:nil] forCellReuseIdentifier:suitablelID];
    [detailsTableView registerNib:[UINib nibWithNibName:@"HMUpgradeTableViewCell" bundle:nil] forCellReuseIdentifier:upgradeID];
    
}

//创建底部视图
- (void)createChooseVeiw
{
    UIView *chooseView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 44, SCREEN_WEIGHT, 44)];
        [self.view addSubview:chooseView];
    
    chooseView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //试读按钮
    UIButton *tryBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 5,chooseView.bounds.size.width * 0.5 - 5 * 2, chooseView.bounds.size.height - 5 * 2)];
    tryBtn.layer.cornerRadius = tryBtn.bounds.size.height * 0.2;
    tryBtn.backgroundColor = [UIColor whiteColor];
    [tryBtn setTitle:@"试读" forState:UIControlStateNormal];
    [tryBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [chooseView addSubview:tryBtn];
    
    [tryBtn addTarget:self action:@selector(tryBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //订阅按钮
    UIButton *subscribeBtn = [[UIButton alloc] initWithFrame:CGRectMake(5 * 2 + tryBtn.bounds.size.width, 5,chooseView.bounds.size.width * 0.5 - 5 * 2, chooseView.bounds.size.height - 5 * 2)];
    subscribeBtn.layer.cornerRadius = subscribeBtn.bounds.size.height * 0.2;
    subscribeBtn.backgroundColor = [UIColor orangeColor];
    [subscribeBtn setTitle:@"订阅" forState:UIControlStateNormal];
    [subscribeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [chooseView addSubview:subscribeBtn];
    

    
     [subscribeBtn addTarget:self action:@selector(subscribeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

//试读按钮跳转
- (void)tryBtnClick:(UIButton *)btn
{
    HMTryReadTableViewController *vc = [[HMTryReadTableViewController alloc] init];
    
    vc.tid = self.dataModel.suitableID;
    
    [self.navigationController pushViewController:vc animated:YES];
}

//订阅按钮跳转
- (void)subscribeBtnClick:(UIButton *)btn
{

    [self createSubscribeBtn];
    
    [self mo_presentContentView:self.btnView option:BottomPop completion:nil];
    
    HMShoppingCarTableViewController *vc = [[HMShoppingCarTableViewController alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

//创建弹出框
- (void)createSubscribeBtn
{
    UIView *btnView = [[UIView alloc] init];
    self.btnView = btnView;
    [self.view addSubview:btnView];
    
    btnView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 550, [UIScreen mainScreen].bounds.size.width, 400);
    
    btnView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel hm_labelWithText:@"您将订阅2017-1-9至2018-1-8的内容,订阅后不支持退订、转让,请再次确认" fontSize:15 color:[UIColor darkGrayColor]];
    [btnView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btnView);
//        make.width.offset(100);
        make.left.offset(40);
        make.right.offset(-40);
        make.height.offset(40);
        make.top.offset(10);
    }];
    titleLabel.numberOfLines = 0;
    
    UIButton *buyBtn = [[UIButton alloc] init];
    buyBtn.backgroundColor = [UIColor orangeColor];
    [btnView addSubview:buyBtn];
    [buyBtn setTitle:@"余额支付" forState:UIControlStateNormal];
    [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.centerX.equalTo(self.view);
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.left.offset(20);
        make.right.offset(-20);
        make.height.offset(40);
        
    }];
    
    [buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cancelBtn = [[UIButton alloc] init];
    cancelBtn.backgroundColor = [UIColor grayColor];
    [btnView addSubview:cancelBtn];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(buyBtn.mas_bottom).offset(20);
        make.left.offset(20);
        make.right.offset(-20);
        make.height.offset(40);
    }];
    
    [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)buyBtnClick:(UIButton *)btn
{
//    HMPayViewController *vc = [[HMPayViewController alloc] init];
//    
//    [self dismissViewControllerAnimated:YES completion:nil];
//    
//    [self.navigationController pushViewController:vc animated:YES];
    [self showHudAuto:@"余额不足,请充值"];
    
    
}

- (void)cancelBtnClick:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}




//滚动时调用 0 -> 150 = 0 - > 1;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset = self.detailsTableView.contentOffset.y ;
    self.offset = offset;
    
    //状态栏
    if (self.offset > 20)
    {
        self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    }
    else
    {
         self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    }
    
    //计算透明比
    CGFloat index = offset/ (HEADER_HEINGT - 64);
//    NSLog(@"%f",self.detailsTableView.contentOffset.y);
    
    //修改透明度
    if (index < 1)
    {
        self.navigationBarView.alpha = index;
    }
    
    
    //图片放大
//      CGRectMake(0, 0, self.view.bounds.size.width, HEADER_HEINGT)
    CGFloat imageH = HEADER_HEINGT;
    CGFloat imageW = SCREEN_WEIGHT;
    if (offset < 0)
    {
        CGFloat totalOffset = imageH + ABS(offset);
        CGFloat f = totalOffset / imageH;
        self.detailsHeaderView.detailmageView.frame = CGRectMake(-(imageW * f - imageW) * 0.5, offset, imageW * f, totalOffset);
    }
    else
    {
        self.detailsHeaderView.detailmageView.frame = CGRectMake(0, 0,SCREEN_WEIGHT, 331);
    }
    
}

//数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataModel.last_items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    HMUpgradeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:upgradeID forIndexPath:indexPath];
    cell.upgradeModel = self.dataModel.last_items[indexPath.row];
    return cell;
    
}


@end
